package pers.qianyu.util;

import org.junit.*;
import pers.qianyu.util.model.*;

import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-20 12:26
 */
public class BinTreeUtil {
    /**
     * 根据 Leetcode 样例数组新建二叉树
     */
    public static TreeNode generateBinTree(Integer[] arr) {
        if (arr == null) return null;
        int len = arr.length;
        if (len == 0) return null;
        TreeNode root = new TreeNode(arr[0]);
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        int cur = 1;
        while (!q.isEmpty() && cur < len) {
            TreeNode node = q.poll();
            if (arr[cur] != null) {
                node.left = new TreeNode(arr[cur]);
                q.offer(node.left);
            }
            cur++;
            if (cur < len && arr[cur] != null) {
                node.right = new TreeNode(arr[cur]);
                q.offer(node.right);
            }
            cur++;
        }
        return root;
    }

    /**
     * 按照 leetcode 的规范打印二叉树
     */
    public static String binTreeToString(TreeNode root) {
        if (root == null) return "";
        Queue<TreeNode> q = new LinkedList<>();
        StringBuilder builder = new StringBuilder();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            if (q.stream().noneMatch(Objects::nonNull)) {
                // 如果这一层都是 null 的话，不用遍历，直接跳出
                break;
            }
            for (int i = 0; i < size; i++) {
                TreeNode cur = q.poll();
                if (cur == null) {
                    builder.append("null").append(",");
                } else {
                    builder.append(cur.val).append(",");
                    q.offer(cur.left);
                    q.offer(cur.right);
                }
            }
        }
        return "[ " + builder.substring(0, builder.length() - 1) + " ]";
    }

    @Test
    public void testGenerateBinTree() {
        TreeNode root = BinTreeUtil.generateBinTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        System.out.println(root);
    }

    @Test
    public void testGenerateBinTree2() {
        TreeNode root = BinTreeUtil.generateBinTree(new Integer[]{1, null, 2, 3});
        System.out.println(root);
    }

    @Test
    public void testBinTreeToString() {
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node2 = new TreeNode(2, node4, node5);
        TreeNode node3 = new TreeNode(3, node6, node7);
        TreeNode node1 = new TreeNode(1, node2, node3);
        System.out.println(BinTreeUtil.binTreeToString(node1));
    }

    @Test
    public void testBinTreeToString2() {
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3, node6, node7);
        TreeNode node1 = new TreeNode(1, node2, node3);
        System.out.println(BinTreeUtil.binTreeToString(node1));
    }

    @Test
    public void testBinTreeToString3() {
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node3 = new TreeNode(3, node6, node7);
        TreeNode node1 = new TreeNode(1, null, node3);
        System.out.println(BinTreeUtil.binTreeToString(node1));
    }
}
